﻿using System;
using System.Linq;
using QDT.Core;
using QDT.Core.Projects;

namespace QDT.Services.Projects {
    public class ProjectTypeService : ServiceBase, IProjectTypeService {

        #region Implementation of IProjectTypeService

        /// <summary>
        ///  添加项目类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ProjectType CreateType(ProjectType type) {

            DbProvider.Add(type);

            DbProvider.SaveChanges();

            return type;
        }

        /// <summary>
        ///  修改项目类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public void UpdateType(ProjectType type) {
            
            DbProvider.Update(type);

            DbProvider.SaveChanges();
        }

        /// <summary>
        /// 删除项目类型
        /// </summary>
        /// <param name="id">项目类型的ID数组</param>
        public void DeleteType(int[] id) {

            foreach (var i in id) {
                var type = DbProvider.GetByID<ProjectType>(i);

                if (type == null) continue;

                var projectCount = DbProvider.D<Project>().Count(x => x.TypeID == type.ID);

                //是否存在相关的项目信息，如果存在将不允许永久删除，应先删除项目
                if (projectCount > 0) throw new ProjectTypeException(ProjectTypeException.ExistProjectsForDelete, type);

                type.IsDelete = true;
            }

            DbProvider.SaveChanges();
        }

        /// <summary>
        /// 从回收站恢复项目类型
        /// </summary>
        /// <param name="id"></param>
        public void RestoreType(int[] id) {

            foreach (var i in id) {

                var type = DbProvider.GetByID<ProjectType>(i);

                if (type == null) continue;

                type.IsDelete = false;
            }
            DbProvider.SaveChanges();
        }

        /// <summary>
        /// 永久的删除项目类型
        /// </summary>
        /// <param name="id"></param>
        public void ForeverDeleteType(int[] id) {

            foreach (var i in id) {

                var type = DbProvider.GetByID<ProjectType>(i);

                if (type == null) continue;

                DbProvider.Delete(type);
            }

            DbProvider.SaveChanges();
        }

        /// <summary>
        /// 创建项目类型的扩展字段
        /// </summary>
        /// <param name="feild"></param>
        /// <returns></returns>
        public ProjectFeild CreateFeild(ProjectFeild feild) {

            var duplicate=DbProvider.D<ProjectFeild>().Count(x => x.TypeID == feild.TypeID && x.InnerName == feild.InnerName);

            if(duplicate>0) throw new ProjectFeildException(ProjectFeildException.DuplicateName,feild);

            DbProvider.Add(feild);

            DbProvider.SaveChanges();

            return feild;

        }

        /// <summary>
        /// 更新项目类型的扩展字段
        /// </summary>
        /// <param name="feild"></param>
        /// <returns></returns>
        public void UpdateFeild(ProjectFeild feild) {

            var duplicate =
                DbProvider.D<ProjectFeild>().Count(
                    x => x.ID != feild.ID && x.TypeID == feild.TypeID && x.Name == feild.Name);

            if(duplicate>0) throw new ProjectFeildException(ProjectFeildException.DuplicateName,feild);

            DbProvider.Update(feild);

            DbProvider.SaveChanges();
        }

        /// <summary>
        /// 删除项目类型的扩展字段
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void DeleteFeild(int[] id) {

            foreach (var i in id) {

                var feild = DbProvider.GetByID<ProjectFeild>(i);

                if (feild == null) continue;

                DbProvider.Delete(feild);
            }

            DbProvider.SaveChanges();
        }

        #endregion
    }
}